package com.work.servlet.teacher;

import com.work.bean.Middle;
import com.work.bean.Student;
import com.work.service.MiddleService;
import com.work.service.MyStudentService;
import com.work.service.WorkService;
import com.work.service.impl.MiddleServiceImpl;
import com.work.service.impl.MyStudentServiceImpl;
import com.work.service.impl.WorkServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/7.
 */
@WebServlet("/distribute_work")
public class DistributeWorkServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (req.getParameter("workId") != null && Integer.parseInt(req.getParameter("workId")) != 0){

            String[] strings = req.getParameterValues("checkbox");

            MyStudentService studentService = new MyStudentServiceImpl();
            MiddleService service = new MiddleServiceImpl();
            WorkService workService = new WorkServiceImpl();

            for (int i = 0; i < strings.length; i++) {

                System.out.println(strings[i]+"strings----------------");

                List<Student> students = studentService.findByClassId(strings[i]);

                for (Student s : students) {
                    service.insertMiddle(new Middle(s.getId(), Integer.parseInt(req.getParameter("workId"))));
                }
            }

            workService.updateDistribute(1, Integer.parseInt(req.getParameter("workId")));
            req.getSession().setAttribute("workId",req.getParameter("workId"));

            resp.sendRedirect("/history_work");

        }



    }
}
